<?php require_once('Connections/db_conn.php'); ?>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<?php
mysql_select_db($database_db_conn, $db_conn);
$query_Community_Partner = "SELECT ORGNAME, ORGID FROM organization ORDER BY ORGNAME ASC";
$Community_Partner = mysql_query($query_Community_Partner, $db_conn) or die(mysql_error());
$row_Community_Partner = mysql_fetch_assoc($Community_Partner);
$totalRows_Community_Partner = mysql_num_rows($Community_Partner);

$presenter_id=$_GET['pid'];
$workshop_id=$_GET['id'];
mysql_select_db($database_db_conn, $db_conn);
$query_Presenter = "SELECT * FROM presenter WHERE PRESENTERID = $presenter_id";
$Presenters= mysql_query($query_Presenter, $db_conn) or die(mysql_error());
$row_Presenter = mysql_fetch_assoc($Presenters);

mysql_select_db($database_db_conn, $db_conn);
$query_Presenter_Language = "SELECT LANGID FROM presenterlanguage WHERE PRESENTERID = $presenter_id";
$Presenter_Language = mysql_query($query_Presenter_Language, $db_conn) or die(mysql_error());
$row_Presenter_Language = mysql_fetch_assoc($Presenter_Language);
$totalRows_Presenter_Language = mysql_num_rows($Presenter_Language);

$query_Language = "SELECT lang.LANGID LANGID, lang.LANG LANG, presentp.LANGID CHK FROM `language` lang left join `presenterlanguage` presentp ".
				   "on lang.LANGID = presentp.LANGID and presentp.presenterid=$presenter_id ORDER BY LANG ASC";
$Language = mysql_query($query_Language, $db_conn) or die(mysql_error());

$layout = explode('&&&', file_get_contents('layout.html'));
 echo $layout[0];

session_start();
if (!(isset($_SESSION['username'])))
	header("location:login.php");
?>

<html>
<body>

<!-- Begin Main Column -->

<div id="mainContent">

	<h2>Edit Presenter</h2>
	<form id="form1" method="post" action="">
	  <table width="470" height="286" border="0">
        <tr>
          <td width="184">First Name:</td>
          <td width="276"><label>
            <input type="text" name="first_name" value = "<?php echo $row_Presenter['FIRSTNAME']?>"id="first_name" />
          </label></td>
        </tr>
        <tr>
          <td>Last Name:</td>
          <td><label>
            <input type="text" name="last_name" value = "<?php echo $row_Presenter['LASTNAME']?>" id="last_name" />
          </label></td>
        </tr>
        <tr>
          <td>Address:</td>
          <td><label>
            <input type="text" name="address" value = "<?php echo $row_Presenter['ADDRESS']?>" id="address" />
          </label></td>
        </tr>
        <tr>
          <td>City:</td>
          <td><label>
            <input type="text" name="city" value = "<?php echo $row_Presenter['CITY']?>" id="city" />
          </label></td>
        </tr>
        <tr>
          <td>Zipcode:</td>
          <td><label>
            <input name="zipcode" type="text" value = "<?php echo $row_Presenter['ZIPCODE']?>" id="zipcode" size="5" maxlength="5" />
          </label></td>
        </tr>
        <tr>
          <td>Phone:</td>
          <td><label>
            <input name="phone" type="text" value = "<?php echo $row_Presenter['PHONENO']?>" id="phone" size="10" maxlength="10" />
          </label></td>
        </tr>
        <tr>
          <td>Email:</td>
          <td><label>
            <input type="text" name="email" value = "<?php echo $row_Presenter['EMAIL']?>" id="email" />
          </label></td>
        </tr>
        <tr>
          <td>Fax:</td>
          <td><label>
            <input name="fax" type="text" value = "<?php echo $row_Presenter['FAX_NO']?>" id="fax" size="10" maxlength="10" />
          </label></td>
        </tr>
        <tr>
          <td>Organization</td>
          <td><label>
            <select name="organization" id="organization">
              <?php
do {
?>
<option value="<?php echo $row_Community_Partner['ORGNAME']?>" <?php

if($row_Community_Partner['ORGID'] == $row_Presenter['ORGID']){ echo "selected";}?>>
<?php echo $row_Community_Partner['ORGNAME']?> </option>
              <?php
} while ($row_Community_Partner = mysql_fetch_assoc($Community_Partner));
  $rows = mysql_num_rows($Community_Partner);
  if($rows > 0) {
      mysql_data_seek($Community_Partner, 0);
	  $row_Community_Partner = mysql_fetch_assoc($Community_Partner);
  }
?>
          </select>
          </label></td>
        </tr>
        <tr>
        <td>Languages:</td>
        <td>
          <?php
			  while ($row_Language = mysql_fetch_assoc($Language)){
			  $pres_lang = ($row_Language['CHK'] == NULL)?"unchecked":"checked";
			  echo "<input type='checkbox' name='presenter_lang[]' value='".$row_Language['LANGID']."' $pres_lang />   ".$row_Language['LANG']."<br />";
			  }


	  ?>
        Other:
        <input type="text" name="language_other" id="language" />
        </td>
      </tr>
      <tr>
        <td>Contract Trainer:</td>
        <td>
        <?php
					  		if($row_Presenter['CONTRACTTRAINER'] == "Yes")
					  			{?>

					                <input type="radio" name="contract_trainer" value="Yes" id="contract_trainer_yes" checked = "checked" />
									            Yes
									            <input type="radio" name="contract_trainer" value="No" id="contract_trainer_no" />
            									No
					               <?php }
					                else
					                {?>

					                <input type="radio" name="contract_trainer" value="Yes" id="contract_trainer_yes" />
									            Yes
									            <input type="radio" name="contract_trainer" value="No" id="contract_trainer_no" checked = "checked" />
         										   No
					                <?php }
              ?>
        </td>
      </tr>

      </table>
      <p>
	    <label>
	      <input type="submit" name="submit" id="submit" value="Submit" class = "button"/>
        </label>
      </p>
  </form>

     <?php
		if($_POST) {
		ob_start();
		$host="localhost"; // Host name
		$username="root"; // Mysql username
		$password=""; // Mysql password
		$db_name="rtl"; // Database name
		$tbl_name="presenter"; // Table name

		// Connect to server and select databse.
		mysql_connect("$host", "$username", "$password")or die("cannot connect");
		mysql_select_db("$db_name")or die("cannot select DB");


		$first_name=$_POST['first_name'];
		$last_name=$_POST['last_name'];
		$address=$_POST['address'];
		$city=$_POST['city'];
		$zipcode=$_POST['zipcode'];
		$phone=$_POST['phone'];
		$email=$_POST['email'];
		$fax=$_POST['fax'];
		$organization=$_POST['organization'];
		$contract_trainer=$_POST['contract_trainer'];
		$language_other = $_POST['language_other'];
		$presenter_lang = $_POST["presenter_lang"];


			// To protect MySQL injection (more detail about MySQL injection)
		$first_name = stripslashes($first_name);
		$last_name = stripslashes($last_name);
		$address = stripslashes($address);
		$city = stripslashes($city);
		$zipcode = stripslashes($zipcode);
		$phone = stripslashes($phone);
		$email = stripslashes($email);
		$fax = stripslashes($fax);
		$language_other = stripslashes($language_other);
		//$organization=stripslashes($organization);

		$first_name = mysql_real_escape_string($first_name);
		$last_name = mysql_real_escape_string($last_name);
		$address = mysql_real_escape_string($address);
		$city = mysql_real_escape_string($city);
		$zipcode = mysql_real_escape_string($zipcode);
		$phone = mysql_real_escape_string($phone);
		$email = mysql_real_escape_string($email);
		$fax = mysql_real_escape_string($fax);
		$language_other = mysql_real_escape_string($language_other);

		//$organization=mysql_real_escape_string($organization);

		$error=0;

		$get_org_id = mysql_query("SELECT ORGID FROM ORGANIZATION WHERE ORGNAME='$organization'");
		$org_id = mysql_result($get_org_id,0);

		mysql_query("UPDATE $tbl_name SET ORGID = '$org_id', FIRSTNAME = '$first_name', LASTNAME = '$last_name', 
			ADDRESS = '$address', CITY = '$city', ZIPCODE ='$zipcode', PHONENO = '$phone', EMAIL = '$email', FAX_NO = '$fax',
			CONTRACTTRAINER = '$contract_trainer' WHERE PRESENTERID = $presenter_id");

		$lang_delete = "DELETE FROM PRESENTERLANGUAGE WHERE PRESENTERID = $presenter_id";
        mysql_query($lang_delete, $db_conn);
		if (!empty($presenter_lang))
		{
			foreach ($presenter_lang as $k => $v)
			{
				$sql_lang = "INSERT INTO PRESENTERLANGUAGE (LANGID , PRESENTERID ) VALUES ($v,$presenter_id)";
				if (mysql_query($sql_lang, $db_conn))
					echo "Language has been updated";
				else
				{
					echo("<P>Error adding Language: $sql_lang " . mysql_error() . "</P>");
				}
			}
		}
		$query_Language = "SELECT LANG FROM language";
		$Language = mysql_query($query_Language, $db_conn) or die(mysql_error());
		if($language_other != NULL)
		{
			while ($row_Language = mysql_fetch_assoc($Language)){
				if($row_Language['LANG'] == $language_other)
				{
					$lang_yes = 1;
					break;
				}
				else
				{
					$lang_yes = 0;
				}
			}
			if($lang_yes == 0)
			{
				mysql_query("INSERT INTO language (LANG) VALUES ('$language_other')");
				$get_lang_id=mysql_query("select@@IDENTITY");
				$lang_id=mysql_result($get_lang_id,0);
				mysql_query("INSERT INTO presenterlanguage (LANGID, PRESENTERID) VALUES ('$lang_id', '$presenter_id')");
			}
		}
		if(!$error) 
		{
			if($workshop_id != "")
			{
				echo "<META HTTP-EQUIV='refresh' content='0;URL=EditEvent.php?id=$workshop_id'>";
			}
			else
				echo("<P>Participant has been updated successfully.</P>");
		}
		ob_end_flush();
   }
   ?>

</div>

<!-- Begin Side Column -->
<!-- Begin Footer -->

<?php
echo $layout[1];
?>

</body>
</html>
<?php
mysql_free_result($Community_Partner);


?>
